{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using ExprOptimization, ExprRules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprRules.Grammar(Any[:(Real + Real), 1, 2, 3], Symbol[:Real, :Real, :Real, :Real], Bool[false, true, true, true], Bool[false, false, false, false], Dict(:Real=>[1, 2, 3, 4]), Array{Symbol,1}[Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar = @grammar begin\n",
    "    Real = Real + Real\n",
    "    Real = |(1:3)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loss (generic function with 1 method)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function loss(tree::RuleNode, grammar::Grammar)\n",
    "    ex = get_executable(tree, grammar)\n",
    "    eval(ex)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search: \u001b[1mC\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1ms\u001b[22m\u001b[1ms\u001b[22m\u001b[1mE\u001b[22m\u001b[1mn\u001b[22m\u001b[1mt\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mp\u001b[22m\u001b[1my\u001b[22m\u001b[1mP\u001b[22m\u001b[1ma\u001b[22m\u001b[1mr\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22m\u001b[1ms\u001b[22m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "```\n",
       "CrossEntropyParams\n",
       "```\n",
       "\n",
       "Parameters for Cross Entropy method.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iiterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `top_k::Int`: top k elite samples used in selection\n",
       "  * `p_init::Float64`: initial value when fitting MLE\n",
       "  * `init_method::InitializationMethod`: Initialization method\n"
      ],
      "text/plain": [
       "```\n",
       "CrossEntropyParams\n",
       "```\n",
       "\n",
       "Parameters for Cross Entropy method.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iiterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `top_k::Int`: top k elite samples used in selection\n",
       "  * `p_init::Float64`: initial value when fitting MLE\n",
       "  * `init_method::InitializationMethod`: Initialization method\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?CrossEntropyParams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.CrossEntropy.CrossEntropyParams(20, 5, 6, 5, 100.0, ExprOptimization.CrossEntropy.RandomInit())"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "p = CrossEntropyParams(20, 5, 6, 5, 100.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12-element Array{ExprRules.RuleNode,1}:\n",
       " 1{2,2}\n",
       " 1{2,3}\n",
       " 1{2,4}\n",
       " 1{3,2}\n",
       " 1{3,3}\n",
       " 1{3,4}\n",
       " 1{4,2}\n",
       " 1{4,3}\n",
       " 1{4,4}\n",
       " 2,    \n",
       " 3,    \n",
       " 4,    "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using ExprOptimization.CrossEntropy\n",
    "pop = collect(ExpressionIterator(grammar,2,:Real))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ProbabilisticExprRules.ProbabilisticGrammar(ExprRules.Grammar(Any[:(Real + Real), 1, 2, 3], Symbol[:Real, :Real, :Real, :Real], Bool[false, true, true, true], Bool[false, false, false, false], Dict(:Real=>[1, 2, 3, 4]), Array{Symbol,1}[Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[]]), Dict(:Real=>[0.25, 0.25, 0.25, 0.25]))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pcfg=ProbabilisticExprRules.ProbabilisticGrammar(grammar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ProbabilisticExprRules.ProbabilisticGrammar(ExprRules.Grammar(Any[:(Real + Real), 1, 2, 3], Symbol[:Real, :Real, :Real, :Real], Bool[false, true, true, true], Bool[false, false, false, false], Dict(:Real=>[1, 2, 3, 4]), Array{Symbol,1}[Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[]]), Dict(:Real=>[0.3, 0.233333, 0.233333, 0.233333]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ProbabilisticExprRules.fit_mle!(pcfg, pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       " 0.3     \n",
       " 0.233333\n",
       " 0.233333\n",
       " 0.233333"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[9/30, 7/30, 7/30, 7/30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ProbabilisticExprRules.ProbabilisticGrammar(ExprRules.Grammar(Any[:(Real + Real), 1, 2, 3], Symbol[:Real, :Real, :Real, :Real], Bool[false, true, true, true], Bool[false, false, false, false], Dict(:Real=>[1, 2, 3, 4]), Array{Symbol,1}[Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[]]), Dict(:Real=>[0.28, 0.24, 0.24, 0.24]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ProbabilisticExprRules.fit_mle!(pcfg, pop, 5.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       " 0.28\n",
       " 0.24\n",
       " 0.24\n",
       " 0.24"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[14/50, 12/50, 12/50, 12/50]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ExprOptimization.ProbabilisticExprRules.ProbabilisticGrammar(ExprRules.Grammar(Any[:(Real + Real), 1, 2, 3], Symbol[:Real, :Real, :Real, :Real], Bool[false, true, true, true], Bool[false, false, false, false], Dict(:Real=>[1, 2, 3, 4]), Array{Symbol,1}[Symbol[:Real, :Real], Symbol[], Symbol[], Symbol[]]), Dict(:Real=>[0.25, 0.25, 0.25, 0.25]))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ProbabilisticExprRules.uniform!(pcfg)\n",
    "pcfg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
